package dse.messages.updatingshares;

import dse.*;
import dse.messages.*;

public class ActivateUpdateShareMessageHandler extends MessageSender implements
		MessageHandler {

	private DataObject dataObject;
	private int shareIndex;
	private ActivateUpdateShareRequester requester;
	
	public ActivateUpdateShareMessageHandler(Experiment experiment, DataObject dobj, 
			int shareIndex, ActivateUpdateShareRequester requester)
	{
		super(experiment);
		this.dataObject = dobj;
		this.shareIndex = shareIndex;
		this.requester = requester;
	}
	@Override
	public boolean acceptMessage(Message message) {
		// TODO Auto-generated method stub
		return true;
	}

	@Override
	public void messageInterupted(ExperimentRecorder recorder, Message message,
			double timeLeft) {
		// TODO Auto-generated method stub
		recorder.debug(this, "msgIntrpt: " + message, timeLeft);
		requester.shareCouldNotBeActivated(recorder, dataObject, shareIndex, message.to(), timeLeft);
	}

	@Override
	public void messageReceived(ExperimentRecorder recorder, Message message,
			double timeLeft) {
		// TODO Auto-generated method stub
		recorder.debug(this, "msgRcvd: " + message, timeLeft);
		if (message.messageType() != MessageType.ActivateUpdateShare) {
			recorder.warn(
					"ActivateUpdateShareMessageHandler recieved " + 
					message.messageType() + " message."
					);
			return;
		}
		//message.to().setSequence(Math.max(message.to().getSequence()+1, message.from().getSequence()));
		requester.shareActivated(recorder, dataObject, shareIndex, message.to(), timeLeft);
	}
}
